using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {
    internal class InspectorField_Transition : InspectorField<AGAFieldModel_Transition> {

        Foldout foldout;
        IntegerField sourceStateField;
        IntegerField targetStateField;
        FloatField durationField;
        protected override void BuildUI() {
            foldout = new Foldout() {
                text = labelText
            };
            sourceStateField = new IntegerField("source state");
            sourceStateField.RegisterValueChangedCallback(evt => OnModify());
            sourceStateField.value = source.sourceState;

            targetStateField = new IntegerField("target state");
            targetStateField.RegisterValueChangedCallback(evt => OnModify());
            targetStateField.value = source.targetState;

            durationField = new FloatField("duration");
            durationField.RegisterValueChangedCallback(evt => OnModify());
            durationField.value = source.duration;
        }

        protected override void DrawUI() {
            Add(foldout);
            foldout.Add(sourceStateField);
            foldout.Add(targetStateField);
            foldout.Add(durationField);
        }

        protected override void ClearUI() {
            foldout?.Clear();
            Clear();
        }

        private void OnModify() {
            source.sourceState = sourceStateField.value;
            source.targetState = targetStateField.value;
            source.duration = durationField.value;

            inspectorChangeCallback?.Invoke();
            ClearUI();
            DrawUI();
        }
    }
}
